package test;

import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
 
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.SwingUtilities;
 
import java.net.URL;
 
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
public class ToolBarPane extends JPanel implements ActionListener {
    protected JTextArea textArea;
    protected String newline = "\n";
    static final private String PREVIOUS = "previous";
    static final private String UP = "up";
    static final private String NEXT = "next";
    static final private String SOMETHING_ELSE = "other";
    static final private String TEXT_ENTERED = "text";
 
    public ToolBarPane() {
        super(new BorderLayout());
        JToolBar toolBar = new JToolBar("Still draggable");
        addButtons(toolBar);
        toolBar.setFloatable(false);
        toolBar.setRollover(true);

        add(toolBar, BorderLayout.PAGE_START);

    }
 
    protected void addButtons(JToolBar toolBar) {
        JButton button = null;
 
        //first button
        button = makeNavigationButton("calculator_link", PREVIOUS,
                                      "Back to previous something-or-other",
                                      "Previous");
        toolBar.add(button);
 
        //second button
        button = makeNavigationButton("calendar_add", UP,
                                      "Up to something-or-other",
                                      "Up");
        toolBar.add(button);
 
        //third button
        button = makeNavigationButton("calendar_delete", NEXT,
                                      "Forward to something-or-other",
                                      "Next");
        toolBar.add(button);
 
        //separator
        toolBar.addSeparator();
 
        //fourth button
        button = new JButton("Another button");
        button.setActionCommand(SOMETHING_ELSE);
        button.setToolTipText("Something else");
        button.addActionListener(this);
        toolBar.add(button);
 
       // toolBar.add(textField);
    }
 
    protected JButton makeNavigationButton(String imageName,
                                           String actionCommand,
                                           String toolTipText,
                                           String altText) {
        //Look for the image.
        String imgLocation = "images/"
                             + imageName
                             + ".png";
        URL imageURL = ToolBarPane.class.getResource(imgLocation);
 
        //Create and initialize the button.
        JButton button = new JButton();
        button.setActionCommand(actionCommand);
        button.setToolTipText(toolTipText);
        button.addActionListener(this);
 
        if (imageURL != null) {                      //image found
            button.setIcon(new ImageIcon(imageURL, altText));
        } else {                                     //no image found
            button.setText(altText);
            System.err.println("Resource not found: "
                               + imgLocation);
        }
 
        return button;
    }
 
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
        String description = cmd;
 
        // Handle each button.
        if (PREVIOUS.equals(cmd)) { //first button clicked
            description += "taken you to the previous <something>.";
        } else if (UP.equals(cmd)) { // second button clicked
            description += "taken you up one level to <something>.";
        } else if (NEXT.equals(cmd)) { // third button clicked
            description += "taken you to the next <something>.";
        } else if (SOMETHING_ELSE.equals(cmd)) { // fourth button clicked
            description += "done something else.";
        } else if (TEXT_ENTERED.equals(cmd)) { // text field
            JTextField tf = (JTextField)e.getSource();
            String text = tf.getText();
            tf.setText("");
            description += "done something with this text: "
                          + newline + "  \""
                          + text + "\"";
        }
 
        displayResult("If this were a real app, it would have "
                        + description + "cmd: " + cmd);
    }
 
    protected void displayResult(String actionDescription) {
        System.out.println(actionDescription);
    }
 

}